package com.example.todofinally.sql;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class SpHelper {

    private static SQLiteDatabase mWritableDatabase;
private Context context;
    public SpHelper(Context context) {
        SqLiteHelper mHelper = new SqLiteHelper(context);
        this.context=context;
        mWritableDatabase = mHelper.getWritableDatabase();
    }
    public void inserData(ContentValues contentValues){
       mWritableDatabase.insert("todo",null,contentValues);
       /* //方式2->使用sql语句添加数据
        String sql = "INSERT into person (time,title,text,Photo) values('2022-10-20','打LOL','打LOL',null)";
        mWritableDatabase.execSQL(sql);*/
    }
    public void updateData(ContentValues contentValues,String id){
        mWritableDatabase.update("todo",contentValues,"id=?",new String[]{id});
       /* //方式2->使用sql语句添加数据
        String sql = "INSERT into person (time,title,text,Photo) values('2022-10-20','打LOL','打LOL',null)";
        mWritableDatabase.execSQL(sql);*/
    }
    public void changeFinished(String id){
        ContentValues contentValues=new ContentValues();
        contentValues.put("status",1);
        mWritableDatabase.update("todo",contentValues,"id=?",new String[]{id});
       /* //方式2->使用sql语句添加数据
        String sql = "INSERT into person (time,title,text,Photo) values('2022-10-20','打LOL','打LOL',null)";
        mWritableDatabase.execSQL(sql);*/
    }
    public void changeUnfinished(String id){
        ContentValues contentValues=new ContentValues();
        contentValues.put("status",0);
        mWritableDatabase.update("todo",contentValues,"id=?",new String[]{id});
       /* //方式2->使用sql语句添加数据
        String sql = "INSERT into person (time,title,text,Photo) values('2022-10-20','打LOL','打LOL',null)";
        mWritableDatabase.execSQL(sql);*/
    }
    public void changeOverdue(String id){
        ContentValues contentValues=new ContentValues();
        contentValues.put("status",2);
        mWritableDatabase.update("todo",contentValues,"id=?",new String[]{id});
       /* //方式2->使用sql语句添加数据
        String sql = "INSERT into person (time,title,text,Photo) values('2022-10-20','打LOL','打LOL',null)";
        mWritableDatabase.execSQL(sql);*/
    }
    public void delete(String id) {
      if( mWritableDatabase.delete("todo","id=?", new String[]{id})!=0)
        {
            Toast.makeText(context, "删除事件", Toast.LENGTH_SHORT).show();
        }
       // String sql = "delete from person where id =' " + id + " ' ";
        //mWritableDatabase.execSQL(sql);
    }

    @SuppressLint("Range")
    public List<Data> query1 () {
        List<Data> list=new ArrayList<>();
        Cursor cursor = mWritableDatabase.query("todo",null,null,null,null,null,null);
//        cursor.moveToFirst();
        if (cursor.moveToFirst()) {
            do {
                Data data=new Data();
                data.id=cursor.getInt(cursor.getColumnIndex("id"));
                data.startTime = cursor.getString(cursor.getColumnIndex("startTime"));
              /*  data.endTime = cursor.getString(cursor.getColumnIndex("endTime"));*/
                data.editContent = cursor.getString(cursor.getColumnIndex("content"));
                data.editTitle = cursor.getString(cursor.getColumnIndex("editTitle"));
                data.date =  cursor.getString(cursor.getColumnIndex("date"));
                data.status = cursor.getInt(cursor.getColumnIndex("status"));
                Log.d("遍历", "query1: "+data.id);
                list.add(data);
            }
            while (cursor.moveToNext());
        }
        cursor.close();
        return list;
    }
    @SuppressLint("Range")
    public Data query3 (String id) {
        Data data=new Data();
        Cursor cursor = mWritableDatabase.query("todo",null,"id=?",new String[]{id},null,null,null);
//        cursor.moveToFirst();
        if (cursor.moveToFirst()) {
            do {
                data.id=cursor.getInt(cursor.getColumnIndex("id"));
                data.startTime = cursor.getString(cursor.getColumnIndex("startTime"));
                /*data.endTime = cursor.getString(cursor.getColumnIndex("endTime"));*/
                data.editContent = cursor.getString(cursor.getColumnIndex("content"));
                data.editTitle = cursor.getString(cursor.getColumnIndex("editTitle"));
                data.photo = cursor.getString(cursor.getColumnIndex("photo"));
                data.date =  cursor.getString(cursor.getColumnIndex("date"));
                data.status = cursor.getInt(cursor.getColumnIndex("status"));
                Log.d("遍历", "query1: "+data.id);
            }
            while (cursor.moveToNext());
        }
        cursor.close();
        return data;
    }
    @SuppressLint("Range")
    public List<Data> query2 () {
        List<Data> list = new ArrayList<>();
        Cursor cursor = mWritableDatabase.query("todo",null,null,null,null,null,null);
        if (cursor.moveToFirst()) {
            do {
                Data data=new Data();
                data.id=cursor.getInt(cursor.getColumnIndex("id"));
                data.date = cursor.getString(cursor.getColumnIndex("date"));
                data.startTime = cursor.getString(cursor.getColumnIndex("startTime"));
                data.editContent = cursor.getString(cursor.getColumnIndex("content"));
                data.photo = cursor.getString(cursor.getColumnIndex("photo"));
                data.status = cursor.getInt(cursor.getColumnIndex("status"));
                list.add(data);
            }
            while (cursor.moveToNext());
        }
        cursor.close();
        return list;
    }


}
